import Vue from "vue";
import Vuex from "vuex";
import { tomorrow } from "./shared";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    currentIndex: 1,
    todos: [
      {
        icon: "user",
        name: "Personal",
        tasks: [
          {
            id: 1,
            title: "清香白莲素还真",
            date: new Date(),
            done: false,
            deleted: false
          }
        ],
        colors: ["#ff6262", "#ffa947"]
      },
      {
        icon: "suitcase",
        name: "Work",
        tasks: [
          {
            id: 3,
            title: "三年寻龙",
            date: new Date(),
            done: true,
            deleted: false
          },
          {
            id: 4,
            title: "十年点穴",
            date: new Date(),
            done: false,
            deleted: false
          },
          {
            id: 5,
            title: "肩负青囊走南北",
            date: new Date(),
            done: false,
            deleted: false
          },
          {
            id: 6,
            title: "三寸知息",
            date: new Date(),
            done: false,
            deleted: false
          },
          {
            id: 7,
            title: "十面洞心",
            date: new Date(),
            done: false,
            deleted: false
          },
          {
            title: "掌握乾坤通天阙",
            date: new Date("2019-09-16"),
            done: false,
            deleted: false
          }
        ],
        colors: ["#5b9df9", "#47bfff"]
      },
      {
        icon: "home",
        name: "Home",
        tasks: [
          {
            id: 2,
            title: "看莽莽红尘",
            date: new Date(),
            done: false,
            deleted: false
          },
          {
            id: 2,
            title: "谁将韶光偷换",
            date: new Date(),
            done: false,
            deleted: false
          },
          {
            id: 2,
            title: "人也好",
            date: new Date(),
            done: false,
            deleted: false
          },
          {
            id: 2,
            title: "魂也罢",
            date: new Date(),
            done: false,
            deleted: false
          },
          {
            id: 2,
            title: "不过一抹塘荷影",
            date: new Date(),
            done: false,
            deleted: false
          }
        ],
        colors: ["#2c7d59", "#3ba776"]
      }
    ],
    // eslint-disable-next-line no-dupe-keys
    selected: null,
    unselect: null,
    editing: null
  },
  getters: {
    currentTodo(state) {
      return state.todos[state.currentIndex];
    },
    todayTask(state) {
      const tasks = [];
      state.todos.forEach(todo => {
        todo.tasks.forEach(task => {
          if (task.date <= tomorrow && !task.done && !task.deleted) {
            tasks.push(task);
          }
        });
      });
      return tasks;
    }
  },
  mutations: {
    selectTodo(state, selected) {
      state.unselect = null;
      state.selected = selected;
    },
    unselectTodo(state) {
      state.unselect = state.selected;
      state.selected = null;
    },
    nextTodo(state) {
      if (state.currentIndex < state.todos.length - 1) {
        state.currentIndex++;
      }
    },
    prevTodo(state) {
      if (state.currentIndex > 0) {
        state.currentIndex--;
      }
    },
    deleteTask(_, { task }) {
      task.deleted = true;
    },
    toggleEditing(state) {
      if (state.editing && state.editing.text) {
        state.selected.todo.tasks.unshift({
          title: state.editing.text,
          date: new Date(),
          done: false,
          deleted: false
        });
      }
      state.editing = state.editing ? null : { text: "" };
    }
  },
  actions: {}
});
